import 'package:find_friend/common/lifecycle.dart';
import 'package:find_friend/const/const.dart';
import 'package:find_friend/const/theme_notifier.dart';
import 'package:find_friend/pages/guide/guidePage.dart';
import 'package:find_friend/pages/login&register/login.dart';
import 'package:find_friend/root/root_page.dart';
import 'package:find_friend/router.dart';
import 'package:flutter/material.dart';
import 'package:flutter_statusbarcolor/flutter_statusbarcolor.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() {

  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<ThemeChanger>(
      create: (context) => ThemeChanger(),
      child: MaterialAppWithTheme(),
    );
  }
}

class MaterialAppWithTheme extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    final theme = Provider.of<ThemeChanger>(context);
    return MaterialApp(
      title: '找他',
      debugShowCheckedModeBanner: false,
      theme: theme.getTheme(),
      onGenerateRoute: onGenerateRoute,
      home: LifeCycleManager(
        child: Home(),
      ),
    );
  }
}

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {

  // 初始化
  Future<Widget> _loadBaseWidget() async {
    SharedPreferences share = await SharedPreferences.getInstance();
    if(share.getBool(KEY_is_first_app) != true) {
      return GuideScreen();
    } else {
      if(share.getString(KEY_user_token) == null) {
        return LoginScreen();
      } else {
        return RootPage();
      }
    }
  }


  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 375, height: 667, allowFontScaling: true);
    FlutterStatusbarcolor.setStatusBarColor(Colors.transparent, animate: true);
    final size = MediaQuery.of(context).size;
    return FutureBuilder(
          future: _loadBaseWidget(),
          initialData: Container(width: size.width, height: size.height, child: Image.asset("assets/launch.jpg")),
          builder: (context, snapshot){
            return snapshot.data;
          },
        );
  }
}
